<?php
##############################################
#
#   奖品
# @ Time : 2017-1-30 - 下午11:24:18
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Home\Model;

class PrizeRecordModel extends IndexBaseModel{
	/*中奖记录表  */
	protected $tableName = 'prize_record';

	//统计今天已中奖的奖品总数
	public function countRecord($p_id){
		$startTime = strtotime(date('Y-m-d 00:00:00', time()));
        $endTime = strtotime(date('Y-m-d 23:59:59', time()));
        return $this -> where("prize_id = $p_id and (time >= $startTime and time <= $endTime)") -> count();
    }

    //获取该用户抽中实体奖品的总数
    public function getReal($user_id){
		return $this -> where("user_id = $user_id and prize_type = 2") -> count();
    }

    public function getMatterNum($r_id){
    	$model = M();
    	return $model -> table('mt_lottery_rules') -> where("r_id = $r_id") -> getField('matter_num');
    }

    public function getDayNum($r_id){
    	$model = M();
    	return $model -> table('mt_lottery_rules') -> where("r_id = $r_id") -> getField('day_num');
    }

    //已中奖插入记录
    public function insertWinningRecord($arr,$user_id){
        $user = M('users')->where("id = $user_id")->find();
    	$data = array(
    		'user_id' => $user['id'],
			'user_name' => $user['nickname'],
			'mobile' => $user['user_mobile'],
			'activity_id' => 2,
			'activity_name' => '大转盘',
    		'prize_id' => $arr['p_id'],
    		'prize_type' => 1,//$arr['prize_type'],
    		'time' => time()
    	);
    	return $this -> add($data);
    }

    //获取当前用户中奖的奖品
    public function getUserWinningPrize($user_id){
    	$sql = "select pr.prize_name,re.prize_id  from mt_prize_record as `re` left join 
    			mt_prize as `pr` on re.prize_id = pr.p_id where user_id = $user_id";
    	$model = M();
    	return $model->query($sql);
    }

    //获取所有中奖记录
    public function getAllWinningPrize(){
    	$sql = "select pr.prize_name, us.nickname from mt_prize_record as `re` left join 
    			mt_prize as `pr` on re.prize_id = pr.p_id left join
    			mt_users as `us` on re.user_id = us.id  WHERE pr.prize_name NOT LIKE '%谢%' order by re.record_id DESC ";
    	$model = M();
    	return $model->query($sql);
    }

}
